package com.utf999.qiangmanger.controller;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.utf999.qiangmanger.controller.permission.AuthorityAnnotation;
import com.utf999.qiangmanger.controller.permission.AuthorityScope;
import com.utf999.qiangmanger.controller.permission.PromptModeType;
import com.utf999.qiangmanger.entity.JsonEntity;
import com.utf999.qiangmanger.entity.orm.AdminUserEntity;
import com.utf999.qiangmanger.entity.orm.CommodityEntity;
import com.utf999.qiangmanger.entity.orm.CommodityTypeEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.iface.ICommodityService;
import com.utf999.qiangmanger.service.iface.ICommodityTypeService;

/**
 * 涉及到商品、商品类型管理和操作的控制层
 * @author yinwenjie
 *
 */
@Controller
@RequestMapping(value={"/commodity"})
public class CommodityController extends BaseController {
	/**
	 * 
	 */
	@Autowired
	private ICommodityTypeService commodityTypeService;
	
	/**
	 * 
	 */
	@Autowired
	private ICommodityService commodityService;
	
	private static Log LogCommodityController = LogFactory.getLog(CommodityController.class);
	
	/**
	 * 向指定公司添加一个新的商品目录信息
	 * @param request
	 * @param response
	 * @param companyid 指定的公司编号
	 * @param typename 新增的商品目录的名称
	 */
	@RequestMapping(value={"/appendCommodityType"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void appendCommodityType(HttpServletRequest request , HttpServletResponse response , String companyid , String typename) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.commodityTypeService.appendCommodityType(companyid, typename);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 为一个公司添加一个新的商品信息
	 * @param request
	 * @param response
	 * @param companyid 指定的公司编号
	 * @param typeid 指定的商品目录
	 * @param commodityCode 商品编码
	 * @param commodityName 商品名称
	 * @param sortName 商品简称 
	 * @param remark 商品说明信息
	 */
	@RequestMapping(value={"/appendCommodity"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void appendCommodity(HttpServletRequest request , HttpServletResponse response , String companyid , String typeid , String commodityCode , String commodityName , String sortName , String remark) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.commodityService.appendCommodity(companyid, typeid, commodityCode, commodityName, sortName, remark);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 修改指定商品的上架状态：如果是1，则变成0；如果是0则变为1
	 * @param request
	 * @param response
	 * @param groupid 指定的用户自定义分组
	 */
	@RequestMapping(value={"/modifyCommodityStatus"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void modifyCommodityStatus(HttpServletRequest request , HttpServletResponse response , String commodityid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.commodityService.modifyCommodityStatus(commodityid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 修改指定的商品的基本信息。原则是有传入则进行修改；没有则不进行修改
	 * @param request
	 * @param response
	 * @param commodifyid 指定的商品编号
	 * @param commodifyCode 可能进行修改的商品编号
	 * @param commodifyName 可能进行修改的商品名称
	 * @param commodifySort 可能进行修改的商品简称
	 * @param remark 可能进行修改的商品备注
	 */
	@RequestMapping(value={"/modifyCommodity"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void modifyCommodity(HttpServletRequest request , HttpServletResponse response , String commodifyid , String commodifyCode , String commodifyName , String commodifySort , String remark) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.commodityService.modifyCommodity(commodifyid, commodifyCode, commodifyName, commodifySort, remark);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}  catch (Exception e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo("您填写的资料已被其他商品使用，请检查！");
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 商品维护管理界面前的数据准备和跳转页面
	 * @param request
	 * @param response
	 * @return 
	 */
	@RequestMapping(value={"/perCommodityList"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.REDIRECT)
	public ModelAndView perCommodityList(HttpServletRequest request , HttpServletResponse response) {
		return new ModelAndView("commodity/commoditylist");
	}
	
	/**
	 * 查询指定的公司下的商品目录信息（按照创建时间进行倒序排列）
	 * @param request
	 * @param response
	 * @param companyid 指定的公司信息
	 */
	@RequestMapping(value={"/queryCommodityTypesByCompany"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommodityTypesByCompany(HttpServletRequest request , HttpServletResponse response , String companyid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<CommodityTypeEntity> types = null;
		try {
			types = this.commodityTypeService.queryCommodityTypesByCompany(companyid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(types);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company"});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 查询相关类型下的商品信息（根据status的值决定是否要涉及到商品状态的判断）
	 * @param typeid 指定的商品类型编号信息
	 * @param status 如果不传入，则无论商品状态如何都要进行查询；如果传入了值就要依据传入的状态进行查询
	 * @param request
	 * @param response
	 */
	@RequestMapping(value={"/queryCommoditysByType"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommoditysByType(HttpServletRequest request , HttpServletResponse response , String typeid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<CommodityEntity> commoditys = null;
		try { 
			commoditys = this.commodityService.queryCommoditysByType(typeid, null);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(commoditys);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "commodityType"});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	} 
	
	/**
	 * 按照商品目录的名字和所属公司信息，查询这个商品目录的基本信息<br>
	 * 这个查询方式通常用于在添加产品目录时确定目录名称是否重复
	 * @param request
	 * @param response
	 * @param typename 指定的商品类型名称
	 * @param companyid 指定的公司编号信息
	 */
	@RequestMapping(value={"/queryCommodityTypeByName"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommodityTypeByName(HttpServletRequest request , HttpServletResponse response , String typename , String companyid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		CommodityTypeEntity commodityType = null;
		try { 
			commodityType = this.commodityTypeService.queryCommodityTypesByName(typename, companyid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(commodityType);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company"});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 查询指定的公司下的商品名称信息
	 * @param request 
	 * @param response 
	 * @param companyid 指定的公司编码
	 * @param commodityName 查询的商品名称信息
	 * @return 
	 * @throws BizException
	 */
	@RequestMapping(value={"/queryCommodityByCommodityName"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommodityByCommodityName(HttpServletRequest request , HttpServletResponse response , String companyid , String commodityName) {
		JsonEntity jsonEntity = new JsonEntity();
		
		CommodityEntity commodity = null;
		try { 
			commodity = this.commodityService.queryCommodityByCommodityName(companyid, commodityName);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(commodity);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "commodityType"});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 查询指定的公司下的商品编号信息。
	 * @param request 
	 * @param response 
	 * @param companyid 指定的公司编码
	 * @param commodityCode 查询的商品编码
	 * @throws BizException 
	 */
	@RequestMapping(value={"/queryCommodityByCommodityCode"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommodityByCommodityCode(HttpServletRequest request , HttpServletResponse response , String companyid , String commodityCode) {
		JsonEntity jsonEntity = new JsonEntity();
		
		CommodityEntity commodity = null;
		try { 
			commodity = this.commodityService.queryCommodityByCommodityCode(companyid, commodityCode);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(commodity);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "commodityType"});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 查询指定的公司下的商品简称信息
	 * @param request 
	 * @param response 
	 * @param companyid 指定的公司编码
	 * @param sortName 查询的商品简称
	 * @throws BizException
	 */
	@RequestMapping(value={"/queryCommodityBySortName"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommodityBySortName(HttpServletRequest request , HttpServletResponse response , String companyid , String sortName) {
		JsonEntity jsonEntity = new JsonEntity();
		
		CommodityEntity commodity = null;
		try { 
			commodity = this.commodityService.queryCommodityBySortName(companyid, sortName);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(commodity);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "commodityType"});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 按照商品的id信息，查询商品的基本信息（不包括关联的公司信息）
	 * @param request
	 * @param response
	 * @param commodityid 指定的商品编号
	 */
	@RequestMapping(value={"/queryCommodityByid"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommodityByid(HttpServletRequest request , HttpServletResponse response , String commodityid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		CommodityEntity commodity = null;
		try { 
			commodity = this.commodityService.queryCommodityByid(commodityid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(commodity);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "commodityType"});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 使用相似查询，针对商品名称、商品简称进行查询（并在去重后返回）
	 * @param request
	 * @param response
	 * @param likeName 相似查询的关键字
	 */
	@RequestMapping(value={"/queryCommodityLikeName"})
	@AuthorityAnnotation(scope={AuthorityScope.StoreAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommodityLikeName(HttpServletRequest request , HttpServletResponse response ,String likeName) {
		JsonEntity jsonEntity = new JsonEntity();
		
		//获取这个操作者的公司信息
		AdminUserEntity admin = (AdminUserEntity)request.getAttribute("admin");
		String companyid = admin.getCompany().getUid();
		List<CommodityEntity> commoditys = null;
		try { 
			//首先解码
			if(!StringUtils.isEmpty(likeName)) {
				String decodeName = URLDecoder.decode(likeName, "UTF-8");
				commoditys = this.commodityService.queryCommodityLikeName(companyid, decodeName);
				jsonEntity.setData(commoditys);
			} else {
				jsonEntity.setData(commoditys);
			}
			
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			this.writeResponseMsg(response, jsonEntity , new String[]{"company"});
		} catch (BizException e) {
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData(null);
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} catch (Exception e) {
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData(null);
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	
	/**
	 * 使用相似查询，针对商品名称、商品简称进行查询（并在去重后返回）
	 * @param request
	 * @param response
	 * @param likeName 相似查询的关键字
	 */
	@RequestMapping(value={"/queryCommodityByOrderinfo"})
	@AuthorityAnnotation(scope={AuthorityScope.StoreAdmin, AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCommodityByOrderinfo(HttpServletRequest request , HttpServletResponse response ,String orderinfoid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<CommodityEntity> commoditys = null;
		try { 
			commoditys = this.commodityService.queryCommodityByOrderinfo(orderinfoid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(commoditys);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "commodityType"});
		} catch (BizException e) { 
			CommodityController.LogCommodityController.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
}
